package com.complaint.rest;

import com.complaint.rest.dao.ComplaintDAO;
import com.complaint.rest.dao.ComplaintDAOImpl;
import com.complaint.rest.model.Complaint;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 * Created by Alex
 */
@Path("/complaint")
public class ComplaintService {
    @Context
    private UriInfo uriInfo;

    private static ComplaintDAO complaintDAO = new ComplaintDAOImpl();

    @POST
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response createComplaint(Complaint complaint) {
        if (complaint.getId() == null || (complaint.getId() != null && complaint.getId().isEmpty())) {
            Complaint persistedComplaint = complaintDAO.addComplaint(complaint);
            return Response.ok(persistedComplaint).build();
        } else {
            complaintDAO.updateComplaint(complaint);
            return Response.ok(complaint).build();
        }
    }

    @DELETE
    @Path("/{id}")
    public Response deleteComplaint(@PathParam("id") String id) {
        complaintDAO.removeComplaint(complaintDAO.getComplaint(id));
        return Response.noContent().build();
    }

    @GET
    @Path("/{id}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Response getComplaint(@PathParam("id") String id) {
        Complaint complaint = complaintDAO.getComplaint(id);
        return Response.ok(complaint).build();
    }

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Complaints getComplaints() {
        Complaints complaints = new Complaints(complaintDAO.listComplaints());
        return complaints;
    }

    @GET
    @Path("/user/{id}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Complaints getComplaintsByUserId(@PathParam("id") String id) {
        Complaints complaints = new Complaints(complaintDAO.getComplaintsByUserId(id));
        return complaints;
    }
}
